<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加属性</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

<!--    <el-empty description="当前页面正在开发中，敬请期待……"></el-empty>-->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

      <el-form-item label="属性模板" prop="templateId">
        <el-select v-model="ruleForm.templateId" placeholder="请选择">
          <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
<el-form-item label="类型">
      <el-radio v-model="ruleForm.type" label="0">非销售属性
        <el-tooltip content="非销售属性" placement="top" >
        <el-button style="padding: 0px !important;margin: 0px !important; border: none" icon="el-icon-info"></el-button>
      </el-tooltip>
      </el-radio>
      <el-radio v-model="ruleForm.type" label="1">销售属性
        <el-tooltip content="销售属性" placement="top" >
        <el-button  style="padding: 0px !important;margin: 0px !important; border: none !important;" icon="el-icon-info"></el-button>
      </el-tooltip>
      </el-radio>
</el-form-item>
      <el-form-item label="类型">
        <el-radio v-model="ruleForm.inputType" label="0">手动输入</el-radio>
        <el-radio v-model="ruleForm.inputType" label="1">单选</el-radio>
        <el-radio v-model="ruleForm.inputType" label="2">多选</el-radio>
        <el-radio v-model="ruleForm.inputType" label="3">单选(下拉列表)</el-radio>
        <el-radio v-model="ruleForm.inputType" label="4">多选(下拉列表</el-radio>
      </el-form-item>
      <el-form-item label="备选值列表" prop="valueList">
        <el-input v-model="ruleForm.valueList"></el-input>
      </el-form-item>
      <el-form-item label="计量单位" prop="unit">
        <el-input v-model="ruleForm.unit"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>

    </el-form>


  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        name:'',
        templateId:'',
        description:'',
        valueList:'',
        unit:'',
        sort:'',
        type: '1',
        inputType: '1'
      },
      options: [],

      rules: {
        // name: [
        //   {required: true, message: '请输入名称', trigger: 'blur'},
        //   {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        // ],
        // templateId: [
        //   {required: true, message: '请选择', trigger: 'blur'},
        //   {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        // ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url='http://localhost:9080/attributes/add-new';
          console.log('url='+url);
          let fromData=this.qs.stringify(this.ruleForm)
          this.axios.post(url,fromData).then((response)=>{
            console.log("response="+response.data)
            let responserBody =response.data;
            if(responserBody.state==20000){
              this.$message.success("添加成功");
              // this.resetForm(formName);
            }else{
              this.$message.error(responserBody.message);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    selectatid(){
      let url='http://localhost:9080/attributes/list1';
      console.log('url='+url);
      this.axios.get(url).then((response)=>{
        console.log("response="+response.data)
        let responserBody =response.data;
        if(responserBody.state==20000){
          this.options=responserBody.data;
        }else{
          this.$message.error(responserBody.message);
        }
      })

    }
  },
  mounted() {
    this.selectatid();
  }

}
</script>